﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using RPGGameLibrary;

namespace WindowsGame1
{
    public partial  class YingEnginWinForm:Game1
    {
       
#if !XBOX
        
        //==========================================================================
 
        /// <summary>
        /// 使用IWinForm接口为参数创建一个新引擎对象。 
        /// 不要创建这个类的示例，而是应该从这个类继承。
        /// StunXnaGE派生自Microsoft.Xna.Framework.Game，使用方法与Game对象相同。
        /// IWinForm接口使用GraphicsDevice.Present(handle)将绘制重定向到windows窗体的某个控件。
        /// </summary>
        /// <param name="form">IWinForm接口，实现了对winForm和winControl的引用        
        public YingEnginWinForm(IWinForm form)
            : base()
        {
            if (form != null)
            {
                this.winForm = form.Form;
                this.winControl = form.winControl;
                 
                // 添加一些处理事件
                this.winForm.FormClosing += new FormClosingEventHandler(winForm_FormClosing);
                this.winControl.Resize += new EventHandler(winControl_Resize);
            }
        }
 
        /// <summary>
        /// 如果使用Winform窗体，则将显示重新定向到一个窗体控件中
        /// </summary>
        /// <param name="gameTime">
        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
 
            // 绘制光标
           /* spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
            if (scm.isShowMouseCursor)
                spriteBatch.Draw(cursor, new Rectangle(Input.MousePos.X, Input.MousePos.Y, 32, 32), Color.White);
            spriteBatch.End();  
 */
            if (winControl != null)
                GraphicsDevice.Present(null, null,winControl.Handle);
        }
 
        /// <summary>
        /// 处理主窗体的Closing事件
        /// </summary>
        void winForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // 如果主窗体关闭则退出程序
            this.Exit();
        }
 
        /// <summary>
        /// 根据WinForm绘制控件的大小重新设置图形设备的大小。
        /// </summary>
        /// <param name="sender">
        /// <param name="e">
        void winControl_Resize(object sender, EventArgs e)
        {
            Control c = (Control)sender;
            if (c != null)
            {
                graphics.PreferredBackBufferHeight = c.ClientSize.Height > 0 ? c.ClientSize.Height : 2;
                graphics.PreferredBackBufferWidth = c.ClientSize.Width > 0 ? c.ClientSize.Width : 2;
                graphics.ApplyChanges();        
            }
        }
 
        /// <summary>
        /// 在Windows.Forms环境中不需要XNA游戏窗口，所以将它隐藏
        /// </summary>
        /// <param name="sender">
        /// <param name="e">
        public void gameWindowForm_Shown(object sender, EventArgs e)
        {
            this.gameWindowForm.Hide();
            winControl_Resize(winControl, null);
        }    
 
#endif

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            CameraLocation = new Vector3(0, 3, 5);
            Manager.Initialize();
#if !XBOX
            //-----------------------------------------
            //  用于Windows.Forms支持的代码，添加gameWindowForm调用Shown方法时的处理事件
            //-----------------------------------------
            if (winForm != null)
            {
                gameWindowForm = (System.Windows.Forms.Form)System.Windows.Forms.Form.FromHandle(this.Window.Handle);
                gameWindowForm.Shown += new EventHandler(gameWindowForm_Shown);
            }

            //-----------------------------------------
            //  用于Windows.Forms支持的代码：显示主窗体
            //-----------------------------------------
            if (winForm != null)
                winForm.Show();
#endif
            base.Initialize();

        }
    }
}
